# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2JsonByYM

import time
import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


class UserCodeDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()


    def queryUserCodeById(self, id):
        """
        根据ID查询信息    查询    zzh_user_oauth
        :param :
        :return:
        单体查询，按照id查询
        """
        sql = "SELECT * FROM zzh_user_code WHERE id=%s"
        cur = self.conn.cursor()
        total_count = cur.execute(sql,[id])
        if total_count:
            result = cur.fetchall()
            cur.close()
            self.conn.close()
            userOauthJson = Table2JsonByYM.user_code_list_to_json(result)
            return total_count, userOauthJson
        else:
            cur.close()
            self.conn.close()
            return 0, None

    def queryUserCodeByCondition(self, **kwargs):
        """
        条件查询  zzh_user_oauth
        :param :
        :return:
        此处使用userId为条件查询
        """
        userId = kwargs['userId']
        page_index = kwargs['pageIndex']
        PAGE_SIZE = kwargs['pageSize']
        sql = "SELECT * FROM zzh_user_code WHERE user_id=%s"
        cur = self.conn.cursor()
        total_count = cur.execute(sql, [userId])
        if total_count:
            result = cur.fetchall()
            cur.close()
            self.conn.close()
            if (page_index * PAGE_SIZE) < total_count:
                start = (page_index - 1) * PAGE_SIZE
                end = page_index * PAGE_SIZE
                userOauthPart = result[start: end]
                userOauthJSON = Table2JsonByYM.user_code_list_to_json(userOauthPart)
                return total_count, userOauthJSON
            else:
                start = (page_index - 1) * PAGE_SIZE
                userOauthPart = result[start: ]
                userOauthJSON = Table2JsonByYM.user_code_list_to_json(userOauthPart)
                return total_count, userOauthJSON

        else:
            cur.close()
            self.conn.close()
            return 0, None


    def addUserCode(self,Info):
        """

        :param userRechargeInfo:
        :return:
        """
        userId = Info['userId']  # 必填，请传递user_id
        userName = Info['userName']  #
        type = Info['type']
        stringCode = Info['stringCode']  #
        userIp = Info['userIp']
        status = Info['status']#
        effTime = Info['effTime']  #
        count = Info['count']
        creatTime = localTime


        cur = self.conn.cursor()
        sql = 'INSERT INTO zzh_user_code (user_id, user_name, type, str_code, count, status,user_ip,eff_time,create_time) VALUES (%s,%s,%s, %s ,%s, %s, %s,%s, %s)'
        rs=cur.execute(sql,[userId,userName,type,stringCode,count,status,userIp,effTime,creatTime])
        self.conn.commit()
        cur.close()
        self.conn.close()
        if rs:
            return rs
        else:
            return None


    def updateUserCode(self,Info):#
        id = Info['id']
        userId = Info['userId']  # 必填，请传递user_id
        userName = Info['userName']  #
        type = Info['type']
        stringCode = Info['stringCode']  #
        userIp = Info['userIp']
        status = Info['status']#
        effTime = Info['effTime']  #
        count = Info['count']
        cur = self.conn.cursor()
        sql = 'UPDATE zzh_user_code SET user_id=%s, user_name=%s, type=%s, str_code=%s, count=%s, status=%s,user_ip=%s,eff_time=%s WHERE id=%s'
        rs=cur.execute(sql,[userId,userName,type,stringCode,count,status,userIp,effTime,id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        if rs:
            return rs
        else:
            return None

    def selectUserCode(self, id):
        """
        根据ID查询信息，返回元组

        """
        sql = "select * from zzh_user_code WHERE id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [id])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr



    def deleteCode(self, ids):
        """
        根据ID删除数据表信息，ID需要以数组形式传递
        :param adminIdArray: id 构成的数组 []
        :return:
        """
        sql = "delete from zzh_user_code WHERE id=%s"
        cur = self.conn.cursor()
        for id in ids:
            rs = cur.execute(sql, [id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs
